package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.resource.date.DateUtils;
import com.itheima.health.resource.entity.QueryPageBean;
import com.itheima.health.resource.pub.RedisConstant;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.service.SetmealService;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.Setmeal;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service
@Transactional
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {


    @Override
    public void batchOrderSetting(List<String[]> list) {

               //  解析excel 将数据 转换成Java 对象
        List<OrderSetting>  orderSettings = StringTOList(list);
        List<OrderSetting>  orderSettingSave = new ArrayList<OrderSetting>();//   存放没有id 的预约对象
        List<OrderSetting>  orderSettingUpdate = new ArrayList<OrderSetting>();//   存放有id 的预约对象
        //  循环遍历集合  查询数据库表中有无 当前日期记录
        for (OrderSetting orderSetting : orderSettings) {
            Date orderDate = orderSetting.getOrderDate();
            QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<OrderSetting>();
            queryWrapper.eq("ORDERDATE",orderDate);
            OrderSetting orderSettingIsExist = baseMapper.selectOne(queryWrapper);//  根据条件查询单一记录对象
            if(orderSettingIsExist!=null){
                //  批量修改  需要将id  封装到List集合中
                orderSetting.setId(orderSettingIsExist.getId());//     查询id  封装到 添加OrderSetting 对象
                orderSettingUpdate.add(orderSetting);
            }else{
                orderSettingSave.add(orderSetting);
            }
        }
        saveBatch(orderSettingSave);
        updateBatchById(orderSettingUpdate);
    }


    //  优化的查询方案
    @Override
    public List<Map> findOrderSettingsData(String yearAndMonth) {
        String  beginDate  = yearAndMonth+"-1";
        String  endDate  = yearAndMonth+"-31";

        List<Map> mapList =  baseMapper.findOrderSettingsData(beginDate,endDate);

        return mapList;
    }

    @Override
    public void updateOrderSettingData(String dateparam, Integer num) {
        //  update  xxx  set  number  = ?   where  orderdate  = '2020-8-1'
        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<OrderSetting>();
        queryWrapper.eq("ORDERDATE",dateparam);
        OrderSetting orderSetting =  baseMapper.selectOne(queryWrapper);
        orderSetting.setNumber(num);
        //  where 后面条件的封装
        UpdateWrapper<OrderSetting> updateWrapper = new UpdateWrapper<OrderSetting>();
        updateWrapper.eq("ORDERDATE",dateparam);
        update(orderSetting,updateWrapper);

    }


//    @Override
//    public List<Map> findOrderSettingsData(String yearAndMonth) {
//
//        List<Map>  mapList = new ArrayList<Map>();
//        //   2020-8      1       31 最后一天   select  * from  xxx where  orderdate between  2020-8-1  and  2020-8-31
//        String  beginDate  = yearAndMonth+"-1";
//        String  endDate  = yearAndMonth+"-31";
//        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<OrderSetting>();
//        queryWrapper.between("ORDERDATE",beginDate,endDate);
//        List<OrderSetting> listOrderSettings = list(queryWrapper);
//        for (OrderSetting orderSetting : listOrderSettings) {
//            //   {date: 1   ,number  : 200   ,xxx :20}  Map
//            Date orderDate = orderSetting.getOrderDate();  //  2020-8-12
//            Calendar calendar = Calendar.getInstance();
//            calendar.setTime(orderDate);//   当前日期
//            //  获取指定日期   对应 几号
//            int day = calendar.get(Calendar.DAY_OF_MONTH);
////           int day =  orderDate.getDate();
//            Map  map = new HashMap();
//            map.put("date",day);
//            map.put("number",orderSetting.getNumber());
//            map.put("reservations",orderSetting.getReservations());
//            mapList.add(map);
//
//        }
//
//        return mapList;
//    }

    /**
     *   excel读取的数据  转换 List<OrderSetting> 集合对象
     * @param list
     * @return
     */
    private List<OrderSetting> StringTOList(List<String[]> list) {
          if(list!=null&&list.size()!=0){
              List<OrderSetting>  orderSettingsList = new ArrayList<OrderSetting>();
              for (String[] strings : list) {
                  OrderSetting orderSetting = new OrderSetting();
                  orderSetting.setNumber(Integer.parseInt(strings[1]));
                  Date date = DateUtils.parseString2Date(strings[0], "yyyy/MM/dd");
                  orderSetting.setOrderDate(date);
                  orderSetting.setReservations(0);
                  orderSettingsList.add(orderSetting);
              }
              return orderSettingsList;
          }else{
              return  null;
          }
    }
}
